﻿using System;
using System.Configuration;
using System.Linq;
using DevExpress.Xpo;
using DevExpress.Xpo.Logger;
using DevExpress.Xpo.Logger.Transport;
using Moq;
using NUnit.Framework;
using Zero.Core.Domain;
using Zero.Core.Domain.Query;
using Zero.DataLayer;
using Zero.DataLayer.Helper;
using Zero.DataLayer.MAP;
using Zero.DataLayer.Repository;
using Zero.POCO;
using Zero.POCO.Compagne;
using Zero.POCO.Donnees;
using Zero.POCO.Enum.Helper.Support;
using Zero.POCO.Helper;
using Zero.POCO.Helper.Support;
using Zero.POCO.Interfaces;
using Zero.POCO.Support;
using Zero.ServiceProxy;

namespace Zero.Test
{



    [TestFixture]
    public class CompaignTest
    {
        private Mock<IRepository<Compaign, Guid>> mock;
        private Mock<IRepository<Signalitique, Guid>> mockSignalitique;
        private Mock<IRepositoryVague<Vague, int>> mockVague;


        [SetUp]
        public void Start()
        {

            
            var criterion = new Criterion("Libelle", "LaMortQuiTue", CriteriaOperator.Equal);
            var query = new Query();
            query.Add(criterion);
            //var vague = new VagueRepository().FindBy(query);

            mockVague = new Mock<IRepositoryVague<Vague, int>>();
            mockVague.Setup(e => e.FindBy(It.IsAny<int>()))
                .Returns(new Vague()
                {
                    Annee = 2045,
                    DateDebut = new DateTime( 2015,01,01),
                    Libelle = "VagueTest",
                    Mois = 1,
                    Ecran = 15,
                    Evenement = "rien de particulier",
                    NbJour = 30,
                    Numero = 1
                });
            var vague = mockVague.Object.FindBy(0);
            mock = new Mock<IRepository<Compaign, Guid>>();
            mock.Setup(e => e.FindBy(It.IsAny<Guid>())).Returns(CompaignFactory.CreateCompaign(AnnonceurFactory.CreateAnnonceur("annonceur"), TypeCompaign.CourteDuree, vague));

            mockSignalitique = new Mock<IRepository<Signalitique, Guid>>();
            mock.Setup(e => e.FindBy(It.IsAny<Guid>())).Returns(CompaignFactory.CreateCompaign(AnnonceurFactory.CreateAnnonceur("annonceur"), TypeCompaign.CourteDuree, vague));
            
            InitDAL.Init();

           //TODO: Test purpose Delete later
            const string filePath = @"C:\Users\Administrator\Desktop\upload.xlsx";
            //var vagueFacade = new CreationVagueFacade(new CreateVagueClientProxy());
            //var resultat = DonneesFactory.CreateDonnees(filePath, "LaMortQuiTue", new IndividuRepository(), mockVague.Object.FindBy(Guid.NewGuid()), new RepositoryTvCache(), new RepositoryRadioCache(), new RepositoryPresseCache());
         

        }
        [Test]
        public void CanCreateAFqke()
        {
            Assert.AreEqual(1, 1);
        }

        [Test]
        public void CanCreateACompaign()
        {
            var criterion = new Criterion("Libelle", "LaMortQuiTue", CriteriaOperator.Equal);
            var query = new Query();
            query.Add(criterion);
            var vague = mockVague.Object.FindBy(0);



            var comp = CompaignFactory.CreateCompaign(AnnonceurFactory.CreateAnnonceur("annonceur"), TypeCompaign.CourteDuree, vague);
            Assert.IsNotNull(comp);



            var comp2 = CompaignFactory.CreateCompaign(AnnonceurFactory.CreateAnnonceur("annonceur"), TypeCompaign.CourteDuree, vague, marque: AnnonceurFactory.CreateMarque("marque",  TypeMarque.Lessive));
            Assert.IsNotNull(comp2);
            Assert.AreEqual(comp2.Annonceur.Name,"annonceur");
            Assert.AreEqual(comp2.Marque.Name, "marque");
            Assert.AreEqual(comp2.TypeCompaign, TypeCompaign.CourteDuree);
            Assert.AreEqual(comp2.Marque.Type, TypeMarque.Lessive);



            var comp3 = CompaignFactory.CreateCompaign(AnnonceurFactory.CreateAnnonceur("annonceur"), TypeCompaign.CourteDuree,vague);
            Assert.IsNotNull(comp3);
            Assert.AreEqual(comp3.TypeCompaign, TypeCompaign.CourteDuree);
            Assert.AreEqual(comp3.Annonceur.Name, "annonceur");

            




         

        }



        [Test]
        public void CanCreateAMarque()
        {
            var marque = AnnonceurFactory.CreateMarque("marque",TypeMarque.Lessive);
            Assert.IsNotNull(marque);
            Assert.AreEqual(marque.Name,"marque");
            //Assert.AreEqual(marque.Annonceur.Name,"annonceur");
            Assert.AreEqual(marque.Type, TypeMarque.Lessive);
        }

        [Test]public void CanCreateAnAnnonceur()
        {var annonceur = AnnonceurFactory.CreateAnnonceur("annonceur");
            Assert.IsNotNull(annonceur);
            Assert.AreEqual(annonceur.Name, "annonceur");
        }


        [Test]
        public void CanCreateFiltre()
        {
           /* var filtre = FiltreFactory.CreateFiltre(new FiltreTest());
            Assert.AreEqual(filtre.Nom, new FiltreTest().Nom);
            var Criterion = new Criterion("Libelle", "LaMortQuiTue", CriteriaOperator.Equal);
            var Query = new Query();
            Query.Add(Criterion);
            var vague = mockVague.Object.FindBy(Guid.NewGuid());
            var compagne = CompaignFactory.CreateCompaign(AnnonceurFactory.CreateAnnonceur("annonceur"), TypeCompaign.CourteDuree, filtre, vague);
            Assert.AreEqual(compagne.Vague.Libelle, "LaMortQuiTue");*/
            //TODO: mettre encore un max de tests ici
        }
        



        [Test]
        public void CanCreatePlans()
       {
               var compagne=mock.Object.FindBy(Guid.NewGuid());
              CompaignFactory.CreatePlanTv(name: "MonPlanTv", compaign: compagne);
              Assert.AreEqual(compagne.PlanTvs.Count,1);
            var planTv = compagne.PlanTvs.FirstOrDefault();
            if (planTv != null)
                Assert.AreEqual(planTv.Name, "MonPlanTv");

            CompaignFactory.CreatePlanRadio(name: "MonPlanRadio", compaign: compagne);
              Assert.AreEqual(compagne.PlanRadios.Count, 1);
            var orDefault = compagne.PlanRadios.FirstOrDefault();
            if (orDefault != null)
                Assert.AreEqual(orDefault.Name, "MonPlanRadio");

            CompaignFactory.CreatePlanPresse(name: "MonPlanPresse", compaign: compagne);
              Assert.AreEqual(compagne.PlanPresses.Count, 1);
            var planPresse = compagne.PlanPresses.FirstOrDefault();
            if (planPresse != null)
                Assert.AreEqual(planPresse.Name, "MonPlanPresse");

            CompaignFactory.CreatePlanPresse(name: "MonPlanPresse2", compaign: compagne);
              Assert.AreEqual(compagne.PlanPresses.Count, 2);

              const int numQuart = 2;
              var date = DateTime.Now;
              var supportTv=new SupportTV();
              CompaignFactory.CreateInsertionTv(compagne.PlanTvs.FirstOrDefault(), numQuart, supportTv,date);
              var firstOrDefault = compagne.PlanTvs.FirstOrDefault();

            if (firstOrDefault != null)
            {
                var insertionTv = firstOrDefault.InsertionTvs.FirstOrDefault();
                if (insertionTv != null)
                    Assert.AreEqual(insertionTv.NumeroCinQMinute, numQuart);
            }


            const int numQuartR = 2;
            var dateR = DateTime.Now;
            var supportRadio = new SupportRadio();
            CompaignFactory.CreateInsertionRadio(compagne.PlanRadios.FirstOrDefault(), numQuartR, supportRadio, dateR);
            var firstOrDefaultR = compagne.PlanRadios.FirstOrDefault();

            if (firstOrDefaultR != null)
            {
                var insertionRadio = firstOrDefaultR.InsertionRadios.FirstOrDefault();
                if (insertionRadio != null)
                    Assert.AreEqual(insertionRadio.NumeroCinQMinute, numQuart);
            }


            const int numQuartP = 2;
            var datep = DateTime.Now;
            var supportPresse = new SupportPresse();
            CompaignFactory.CreateInsertionPresse(compagne.PlanPresses.FirstOrDefault(), numQuartP, supportPresse, datep);
            var firstOrDefaultP = compagne.PlanPresses.FirstOrDefault();

            if (firstOrDefaultP != null)
            {
                var insertionPresse = firstOrDefaultP.InsertionPresses.FirstOrDefault();
                if (insertionPresse != null)
                    Assert.AreEqual(insertionPresse.Date, datep);
            }
       }
        

        [Test]
        public void InsertPassage()
        {

            var compagne = mock.Object.FindBy(Guid.NewGuid());
            CompaignFactory.CreatePlanTv(name: "MonPlanTv", compaign: compagne);
            Assert.AreEqual(compagne.PlanTvs.Count, 1);
            var @default = compagne.PlanTvs.FirstOrDefault();
            if (@default != null)
                Assert.AreEqual(@default.Name, "MonPlanTv");

            CompaignFactory.CreatePlanRadio(name: "MonPlanRadio", compaign: compagne);
            Assert.AreEqual(compagne.PlanRadios.Count, 1);
            var radio = compagne.PlanRadios.FirstOrDefault();
            if (radio != null)
                Assert.AreEqual(radio.Name, "MonPlanRadio");

            CompaignFactory.CreatePlanPresse(name: "MonPlanPresse", compaign: compagne);
            Assert.AreEqual(compagne.PlanPresses.Count, 1);
            var presse = compagne.PlanPresses.FirstOrDefault();
            if (presse != null)
                Assert.AreEqual(presse.Name, "MonPlanPresse");

            CompaignFactory.CreatePlanPresse(name: "MonPlanPresse2", compaign: compagne);
            Assert.AreEqual(compagne.PlanPresses.Count, 2);

            /* •————————————————————————————————————————————————————————————————————————————————————————————————•
               | string connectionString;                                                                       |
               | using (                                                                                        |
               |     var xpoSession = new Session()                                                             |
               |     {                                                                                          |
               |         ConnectionString =                                                                     |
               |             ((string)config.GetValue("connect", typeof(string))) |
               |     })                                                                                         |
               | {                                                                                              |
               |     var liste = new XPCollection<XpoSupportTV>(xpoSession);                                    |
               | }                                                                                              |
               •————————————————————————————————————————————————————————————————————————————————————————————————• */

            var criterion = new Criterion("Libelle", "El Watan", CriteriaOperator.Equal);
            var query = new Query();
            query.Add(criterion);
            new SupportPresseRepository().FindBy(query);


            var criterionTv = new Criterion("Libelle", "AN Nahar TV", CriteriaOperator.Equal);
            var queryTv = new Query();
            queryTv.Add(criterionTv);
            new SupportTvRepository().FindBy(queryTv);



            var criterionRadio = new Criterion("Libelle", "Chaine 3", CriteriaOperator.Equal);
            var QueryRadio = new Query();
            QueryRadio.Add(criterionRadio);
            var mediaRadio = new SupportRadioRepository().FindBy(QueryRadio);

            var Criterion = new Criterion("Libelle", "El Watan", CriteriaOperator.Equal);
            var Query = new Query();
            Query.Add(Criterion);
            var titreDePresse = new SupportPresseRepository().FindBy(Query);

            var CriterionTv = new Criterion("Libelle", "AN Nahar TV", CriteriaOperator.Equal);
            var QueryTv = new Query();
            QueryTv.Add(CriterionTv);
            var mediaTv = new SupportTvRepository().FindBy(QueryTv);

            const int numQuart = 2;
            var date = DateTime.Now;
            CompaignFactory.CreateInsertionPresse(compagne.PlanPresses.FirstOrDefault(), 5, titreDePresse.FirstOrDefault(), date);
            CompaignFactory.CreateInsertionRadio(compagne.PlanRadios.FirstOrDefault(), numQuart, mediaRadio.FirstOrDefault(), date);
            CompaignFactory.CreateInsertionTv(compagne.PlanTvs.FirstOrDefault(), numQuart, mediaTv.FirstOrDefault(), date);

            var orDefault = compagne.PlanTvs.FirstOrDefault();
            
            PlanTV nullPlanTv = null;
            if (orDefault != null)
                
            Assert.AreEqual(orDefault.InsertionTvs.Count, 1);
            var planPresse = compagne.PlanPresses.FirstOrDefault();
            
            PlanPresse planPresseNull = null;
            if (!Equals(planPresse, planPresseNull))

                
                Assert.AreEqual(planPresse.InsertionPresses.Count, 1);
            var planRadio = compagne.PlanRadios.FirstOrDefault();
            PlanRadio planRadioNull = null;
            if (planRadio != null)
            Assert.AreEqual(planRadio.InsertionRadios.Count, 1);

            var planTv = compagne.PlanTvs.FirstOrDefault();
            if (planTv != null)
            {
                var insertionTv = planTv.InsertionTvs.FirstOrDefault();
                if (insertionTv != null)
                    Assert.AreEqual(insertionTv.SupportTv.Libelle, "AN Nahar TV");
            }
            //TODO: verifier que les plan ramené de la base ne sont pas null

            var firstOrDefault = compagne.PlanTvs.FirstOrDefault();
            
        }

           [Test]
           public void TvEngine()
           {
               /*
               LogManager.SetTransport(new LogServer(52934));
               
               var vague = mockVague.Object.FindBy(Guid.NewGuid());


               var filtre = FiltreFactory.CreateFiltre(new FiltreTest());

               var compagne = CompaignFactory.CreateCompaign(AnnonceurFactory.CreateAnnonceur("annonceur"), TypeCompaign.CourteDuree, filtre,vague);
               Assert.IsNotNull(compagne);
               CompaignFactory.CreatePlanTv(name: "MonPlanTv", compaign: compagne);
               Assert.AreEqual(compagne.PlanTvs.Count, 1);
               Assert.AreEqual(compagne.PlanTvs.FirstOrDefault().Name, "MonPlanTv");
               /* •——————————————————————————————————————————————————————————————————————————————————————————————————•
                  | string ConnectionString;                                                                          |
                  | using (                                                                                           |
                  |     var xpoSession = new Session()                                                                |
                  |     {                                                                                             |
                  |         ConnectionString =                                                                        |
                  |             ((string)config.GetValue("connect", typeof(string))) |
                  |     })                                                                                            |
                  | {                                                                                                 |
                  |     var liste = new XPCollection<XpoSupportTV>(xpoSession);                                       |
                  | }                                                                                                 |
                  •———————————————————————————————————————————————————————————————————————————————————————————————————• */

           /* var CriterionTv = new Criterion("Libelle", "AN Nahar TV", CriteriaOperator.Equal);
            var QueryTv = new Query();
            QueryTv.Add(CriterionTv);
            var mediaTv = new SupportTvRepository().FindBy(QueryTv);

            const int numQuart = 65;
            var date = DateTime.Now;
            const int numQuart1 = 70;
            const int numQuart2 = 75;
            CompaignFactory.CreateInsertionTv(compagne.PlanTvs.FirstOrDefault(), numQuart, mediaTv.FirstOrDefault(), date);
            //Assert.AreEqual(compagne.GetCov(new VagueRepository()),50); 
            CompaignFactory.CreateInsertionTv(compagne.PlanTvs.FirstOrDefault(), numQuart1, mediaTv.FirstOrDefault(), date);
            //Assert.AreEqual(compagne.GetCov(new VagueRepository()), 50); 
            CompaignFactory.CreateInsertionTv(compagne.PlanTvs.FirstOrDefault(), numQuart2, mediaTv.FirstOrDefault(), date);
            Assert.AreEqual(compagne.GetCov(), 50); 
        
            */
        }
        





        [Test]
        public void GetSignalitiques()
        {
           /* var compagne = mock.Object.FindBy(Guid.NewGuid());
         
            CompaignFactory.CreatePlanTv(name: "MonPlanTv", compaign: compagne);*/
        }


#region serveur

        

        [Test]
        public void InsertPubOnDataBase()
        {
            var compagne = mock.Object.FindBy(Guid.NewGuid());
            CompaignFactory.CreatePlanTv(name: "MonPlanTv", compaign: compagne);
            Assert.AreEqual(compagne.PlanTvs.Count, 1);
            var @default = compagne.PlanTvs.FirstOrDefault();
            if (@default != null)
                Assert.AreEqual(@default.Name, "MonPlanTv");

            CompaignFactory.CreatePlanRadio(name: "MonPlanRadio", compaign: compagne);
            Assert.AreEqual(compagne.PlanRadios.Count, 1);
            var radio = compagne.PlanRadios.FirstOrDefault();
            if (radio != null)
                Assert.AreEqual(radio.Name, "MonPlanRadio");

            CompaignFactory.CreatePlanPresse(name: "MonPlanPresse", compaign: compagne);
            Assert.AreEqual(compagne.PlanPresses.Count, 1);
            var presse = compagne.PlanPresses.FirstOrDefault();
            if (presse != null)
                Assert.AreEqual(presse.Name, "MonPlanPresse");

            CompaignFactory.CreatePlanPresse(name: "MonPlanPresse2", compaign: compagne);
            Assert.AreEqual(compagne.PlanPresses.Count, 2);

            

            var criterion = new Criterion("Libelle", "El Watan", CriteriaOperator.Equal);
            var query = new Query();
            query.Add(criterion);
            var titreDePresse = new SupportPresseRepository().FindBy(query);


            var criterionTv = new Criterion("Libelle", "AN Nahar TV", CriteriaOperator.Equal);
            var queryTv = new Query();
            queryTv.Add(criterionTv);
            var mediaTv = new SupportTvRepository().FindBy(queryTv);



            var CriterionRadio = new Criterion("Libelle", "Chaine 3", CriteriaOperator.Equal);
            var QueryRadio = new Query();
            QueryRadio.Add(CriterionRadio);
            var mediaRadio = new SupportRadioRepository().FindBy(QueryRadio);


            const int numQuart = 2;
            var date = DateTime.Now;
            CompaignFactory.CreateInsertionPresse(compagne.PlanPresses.FirstOrDefault(), 5, titreDePresse.FirstOrDefault(), date);
            CompaignFactory.CreateInsertionRadio(compagne.PlanRadios.FirstOrDefault(), numQuart, mediaRadio.FirstOrDefault(), date);
            CompaignFactory.CreateInsertionTv(compagne.PlanTvs.FirstOrDefault(), numQuart, mediaTv.FirstOrDefault(), date);

            var orDefault = compagne.PlanTvs.FirstOrDefault();
            if (orDefault != null)
            
            Assert.AreEqual(orDefault.InsertionTvs.Count, 1);
            var planPresse = compagne.PlanPresses.FirstOrDefault();
            if (planPresse != null)
             Assert.AreEqual(planPresse.InsertionPresses.Count, 1);
            var planRadio = compagne.PlanRadios.FirstOrDefault();
            if (planRadio != null)
            Assert.AreEqual(planRadio.InsertionRadios.Count, 1);

            var planTv = compagne.PlanTvs.FirstOrDefault();
            if (planTv != null)
            {
                var insertionTv = planTv.InsertionTvs.FirstOrDefault();
                if (insertionTv != null)
                    Assert.AreEqual(insertionTv.SupportTv.Libelle, "AN Nahar TV");
            }

            //TODO : faut trouver comment comparer si elles sont pas null
            var firstOrDefault = compagne.PlanTvs.FirstOrDefault();
            new CampagneRepository().Save(compagne);
        }



        [Test]
         public void CanLoadExcel()
         {

             var config = new AppSettingsReader();
             var connLocal = ((string)config.GetValue("connect", typeof(string)));
             var conRemote = @"data source=tcp:t77jri89qh.database.windows.net,1433;initial catalog=insight-algerie;User ID=insight@t77jri89qh;Password=diesirae&123;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;";
             InitDAL.Init();
             const string nomVague = "LaMortQuiTue";
           
             const string filePath = @"C:\DataBase\upload.xlsx";
             //var vagueFacade = new CreationVagueFacade(new CreateVagueClientProxy());
          
         }


        [Test]
        public void AddInsertionTV()
        {


        }


        #endregion

    }
}
